import {useCommonInfoStore} from "../stores/modules/commonInfo";
import {conUtils} from "./conUtils";
import {useUserInfoStore} from "../stores/modules/userInfo";
import {BaseURL} from "./lshttp";
import {ref} from "vue";

export const copyData = (data) => {
    console.log('copy', data)
    uni.setClipboardData({
        data: data.toString(),
        success() {
            uni
                .showToast({
                    title: '复制成功',
                    icon: 'none',
                })
                .then(() => {
                })
        },
    })
}
// 上传凭证截图 跳转美团、饿了么、京东的页面 'sub_inner_pages/evaluate-list/evaluate-list'  'ele-evaluate-order/pages/ele-evaluate-center/ele-evaluate-center.html?spm=a2ogi.24574003.undefined&startTime=1701940975746'
export const toAppletPage = async (shopType, pageType) => {
    const mtOrderUrl = 'pages/orders/orders'
    const mtEvaluateUrl = 'sub_inner_pages/evaluate-list/evaluate-list'
    const mtAppletId = 'wx2c348cf579062e56'
    const mtAppId = 'gh_72a4eb2d4324'
    const elmOrderUrl = 'pages/orderlist/pages/index/index'
    const elmEvaluateUrl = 'ele-evaluate-order/pages/ele-evaluate-center/ele-evaluate-center.html?spm=a2ogi.24574003.undefined&startTime=1701940975746'
    const elmAppletId = 'wxece3a9a4c82f58c9'
    const elmAppId = 'gh_6506303a12bb'

    const jdOrderUrl = 'pages/order/list/index'
    const jdEvaluateUrl = 'pages/order/list/index'
    const jdAppletId = 'wx91d27dbf599dff74'
    const jdAppId = 'gh_45b306365c3d'

    if (shopType === 1) {
        // #ifdef MP
        await uni.openEmbeddedMiniProgram({
            appId: mtAppletId,
            path: pageType === 1 ? mtOrderUrl : mtEvaluateUrl,
        })
        // #endif
        //#ifdef APP-PLUS
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: mtAppId,
                    path: pageType === 1 ? mtOrderUrl : mtEvaluateUrl,
                    type: 0
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    } else if(shopType === 2) {
        // #ifdef MP
        await uni.openEmbeddedMiniProgram({
            appId: elmAppletId,
            path: pageType === 1 ? elmOrderUrl : elmEvaluateUrl,
        })
        // #endif
        //#ifdef APP-PLUS
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: elmAppId,
                    path: pageType === 1 ? elmOrderUrl : elmEvaluateUrl,
                    type: 0
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    }else {
        // if(pageType !== 1){
        //     await uni.showModal({
        //         title: '温馨提示~',
        //         content: '京东外卖评价截图仅APP提供，请到京东APP截图~',
        //         showCancel: false
        //     });
        //     return
        // }


        // #ifdef MP
        await uni.openEmbeddedMiniProgram({
            appId: jdAppletId,
            path: pageType === 1 ? jdOrderUrl : jdEvaluateUrl,
        })
        // #endif
        //#ifdef APP-PLUS
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: jdAppId,
                    path: pageType === 1 ? jdOrderUrl : jdEvaluateUrl,
                    type: 0
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    }
}
// 跳转小程序订单页面复制订单号 shopType：1美团，2饿了么，3京东
export const toMiniAppOrderPage = async (shopType) => {
    const mtOrderUrl = 'pages/orders/orders'
    const mtAppletId = 'wx2c348cf579062e56'
    const mtAppId = 'gh_72a4eb2d4324'

    const elmOrderUrl = 'pages/orderlist/pages/index/index'
    const elmAppletId = 'wxece3a9a4c82f58c9'
    const elmAppId = 'gh_6506303a12bb'

    const jdOrderUrl = 'pages/order/list/index'
    const jdAppletId = 'wx91d27dbf599dff74'
    const jdAppId = 'gh_45b306365c3d'

    if (shopType === 1) {
        // #ifdef MP
        await uni.openEmbeddedMiniProgram({
            appId: mtAppletId,
            path: mtOrderUrl,
        })
        // #endif
        //#ifdef APP-PLUS
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: mtAppId,
                    path: mtOrderUrl,
                    type: 0
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    } else if(shopType === 2) {
        // #ifdef MP
        await uni.openEmbeddedMiniProgram({
            appId: elmAppletId,
            path: elmOrderUrl,
        })
        // #endif
        //#ifdef APP-PLUS
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: elmAppId,
                    path: elmOrderUrl,
                    type: 0
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    }else {
        // #ifdef MP
        await uni.openEmbeddedMiniProgram({
            appId: jdAppletId,
            path: jdOrderUrl,
        })
        // #endif
        //#ifdef APP-PLUS
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: jdAppId,
                    path: jdOrderUrl,
                    type: 0
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    }
}
// 跳转小程序
export const toMiniAppRedPage = async (item) => {
    console.log('item====', item)
    // #ifdef MP
    await uni.openEmbeddedMiniProgram({
        appId: item.appid,
        path: item.path,
    })
    // #endif
    //#ifdef APP-PLUS
    plus.share.getServices(function (res) {
        const wxServices = res.find(i => i.id.toString() === 'weixin')
        if (wxServices) {
            wxServices.launchMiniProgram({
                id: item.original_id,
                path: item.path,
                type: 0
            });
        }
    }, function (res) {
        console.log(JSON.stringify(res));
    });
    // #endif
}

// 跳转商家
export const toMiniAppBusiness = async (shopType, path) => {
    // #ifdef MP
    const id = shopType === 1 ? 'wx2c348cf579062e56' : shopType === 2 ? 'wxece3a9a4c82f58c9' : 'wx91d27dbf599dff74';
    await uni.openEmbeddedMiniProgram({
        appId: id,
        path: path,
    })
    // #endif
    //#ifdef APP-PLUS
    const idApp = shopType === 1 ? 'gh_72a4eb2d4324' : shopType === 2 ? 'gh_6506303a12bb' : 'gh_45b306365c3d';
    plus.share.getServices(function (res) {
        const wxServices = res.find(i => i.id.toString() === 'weixin')
        if (wxServices) {
            wxServices.launchMiniProgram({
                id: idApp,
                path: path,
                type: 0
            });
        }
    }, function (res) {
        console.log(JSON.stringify(res));
    });
    //#endif
}
// 跳转搜索
export const toMiniAppSearch = async (shopType) => {
    // #ifdef MP
    const id = shopType === 1 ? 'wx2c348cf579062e56' : 'wxece3a9a4c82f58c9';
    const path = shopType === 1 ? "packages/index/search/search" : 'ele-search/pages/search/search';
    await uni.openEmbeddedMiniProgram({
        appId: id,
        path: path,
    })
    // #endif
    //#ifdef APP-PLUS
    const idApp = shopType === 1 ? 'gh_72a4eb2d4324' : 'gh_6506303a12bb';
    const pathApp = shopType === 1 ? "packages/index/search/search.html" : "ele-search/pages/search/search.html";
    plus.share.getServices(function (res) {
        const wxServices = res.find(i => i.id.toString() === 'weixin')
        if (wxServices) {
            wxServices.launchMiniProgram({
                id: idApp,
                path: pathApp,
                type: 0
            });
        }
    }, function (res) {
        console.log(JSON.stringify(res));
    });
    //#endif
}
// 跳转搜索
export const toMiniAppSetting = async (shopType) => {
    // #ifdef MP
    const id = shopType === 1 ? 'wxde8ac0a21135c07d' : 'wxece3a9a4c82f58c9';
    const path = shopType === 1 ? "mt/pages/setting/setting" : 'ele-search/pages/search/search';
    await uni.openEmbeddedMiniProgram({
        appId: id,
        path: path,
    })
    // #endif
    //#ifdef APP-PLUS
    const idApp = shopType === 1 ? 'gh_870576f3c6f9' : 'gh_6506303a12bb';
    const pathApp = shopType === 1 ? "mt/pages/setting/setting.html" : "ele-search/pages/search/search.html";
    plus.share.getServices(function (res) {
        const wxServices = res.find(i => i.id.toString() === 'weixin')
        if (wxServices) {
            wxServices.launchMiniProgram({
                id: idApp,
                path: pathApp,
                type: 0
            });
        }
    }, function (res) {
        console.log(JSON.stringify(res));
    });
    //#endif
}
// 跳转客服
export const openService = () => {
    const commonInfoStore = useCommonInfoStore()
    console.log('commonInfoStore.serviceUrl', commonInfoStore.serviceUrl)
    // #ifdef MP
    wx.openCustomerServiceChat({
        //企业微信的企业id
        corpId: commonInfoStore.serviceUrl.applet.appid,
        extInfo: {
            //客服链接
            url: commonInfoStore.serviceUrl.applet.path
        },
    })
    // #endif
    // #ifdef APP-PLUS
    plus.share.getServices(res => {
        const wxServices = res.find(i => i.id.toString() === 'weixin')
        if (wxServices) {
            wxServices.openCustomerServiceChat({
                corpid: commonInfoStore.serviceUrl.app.corpId,
                url: commonInfoStore.serviceUrl.app.url
            })
        }
    }, function (res) {
        console.log(JSON.stringify(res));
    })
    // #endif
    // #ifdef H5
    window.location.href = commonInfoStore.serviceUrl.h5.url
    // #endif
}

export const getToPagePath = (shopType) => {
    const idApp = shopType === 1 ? 'gh_72a4eb2d4324' : shopType === 2 ? 'gh_6506303a12bb' : 'gh_45b306365c3d';
    const pathApp = shopType === 1 ? "packages/index/search/search.html" : shopType === 2 ? "ele-search/pages/search/search.html" : "packages/index/search/search.html";
    return [idApp, pathApp]
}
// 获取跳转的小程序原始id
export const getBuToPagePath = (shopType) => {
    return shopType === 1 ? 'gh_72a4eb2d4324' : shopType === 2 ? 'gh_6506303a12bb' : 'gh_45b306365c3d';
}
// 平台返回订单号路径
export const getOrderNoPath = (shopType) => {
    const mtObj = {
        path:'pages/orders/orders',
        wetId:'wx2c348cf579062e56',
        appId:'gh_72a4eb2d4324',
    }
    const elmObj = {
        path:'pages/orderlist/pages/index/index',
        wetId:'wxece3a9a4c82f58c9',
        appId:'gh_6506303a12bb',
    }
    const jdObj = {
        path:'pages/order/list/index',
        wetId:'wx91d27dbf599dff74',
        appId:'gh_45b306365c3d',
    }
    return shopType === 1 ? mtObj : shopType === 2 ? elmObj : jdObj;
}
export const toPageUtils = async (item) => {
    //console.log("toPageUtils", 'item-------------')

    const userInfoStore = useUserInfoStore()
    if (item.type === 1 && userInfoStore.isLogin()) {
        console.log("--------1--------",item.type)
        if (item.name === "外卖霸王餐") {
            const commonInfoStore = useCommonInfoStore()
            const address = commonInfoStore.address
            // 跳转内置页面
            await uni.navigateTo({
                url: "/pages/search/category?latitude=" + address?.latitude + "&longitude=" + address?.longitude + "&addressStr=" + address?.recommend
            })
            return
        }
        if (item.name === "免费领会员") {
            await uni.navigateTo({
                url: "/pages/member/membertask"
            })
            return
        }
        if (item.name === "我的会员") {
            await uni.navigateTo({
                url: "/pages/member/member"
            })
            return
        }
        if (item.name === "领食福利群") {
            await uni.navigateTo({
                url: '/pages/share/vipgroup'
            })
            return
        }
        if (item.name === "商家入驻") {
            await uni.navigateTo({
                url: "/pagesme/businessinto/businessinto"
            })
            return
        }
        if (item.name === "新手教程") {
            await uni.navigateTo({
                url: "/pages/index/novice"
            })
            return
        }
        const commonInfoStore = useCommonInfoStore()
        const userId = userInfoStore.userInfo?.id
        const city = commonInfoStore.addressDel.city
        // 跳转内置页面?userId=${userId}&city=${city}
        if (item.path.indexOf("?") !== -1) {
            await uni.reLaunch({
                url: item.path + '&userId=' + userId + '&city=' + city
            })
        }else{
            await uni.reLaunch({
                url: item.path + '?userId=' + userId + '&city=' + city
            })
        }
    }
    if (item.type === 2) {
        // 跳转内置底部菜单
        await uni.switchTab({
            url: item.path
        })
    }
    if (item.type === 3 && userInfoStore.isLogin()) {
        console.log("--------3--------")
        // #ifdef MP
        // 跳转网页URl
        if (item.name === "特惠电影票") {
            await uni.navigateTo({
                url: '/pages/index/toweburl?url= https://www.youpiaopiao.cn/h10/#/pages/movie/index?entid=100940&source=APP100940&entpara=d139e536f2fd1966c48d'
            })
            return
        } else {
            // 跳转网页URl
            if (item.path){
                await uni.navigateTo({
                    url: '/pages/index/toweburl?url=' + item.path
                })
            } else {
                await uni.showToast( {
                    title: 'h5网页链接未配置',
                    icon: 'none',
                    duration: 2000
                })
            }

        }
        // #endif
        // #ifdef H5
        // 跳转网页URl
        if (item.path){
            console.log("--------h5--------", item.path)
            window.location.href = item.path
        } else {
            await uni.showToast( {
                title: 'h5网页链接未配置',
                icon: 'none',
                duration: 2000
            })
        }
        // window.location.href = item.path
        // #endif
        // #ifdef APP-PLUS
        // 跳转网页URl
        if (item.path){
            await uni.navigateTo({
                url: '/pages/index/toweburl?url=' + item.path
            })
        } else {
            await uni.showToast( {
                title: 'h5网页链接未配置',
                icon: 'none',
                duration: 2000
            })
        }
        // #endif
    }
    if (item.type === 4) {
        console.log("toPageUtils4", item)
        await toMiniAppRedPage(item)
    }
}

export const toChallenge = () => {
    const useUser = useUserInfoStore()
    if (useUser.isLogin()) {
        // #ifdef H5
        const commonInfoStore = useCommonInfoStore()
        const city = commonInfoStore.addressDel.city
        uni.navigateTo({url: '/challenge/challenge?city=' + city})
        // #endif
        // #ifdef APP-PLUS || MP
        uni.navigateTo({
            // 正式服
            // url: '/pages/index/toweburl?url=' + "https://lsz.lszbg.com/#/challenge/challenge"
            // 测试服
            url: '/pages/index/toweburl?url=' + "https://test.lszbg.cn/#/challenge/challenge"
            // 不使用H5的情况下内部页面跳转
            // url: '/pages/makemoney/getmaker'
        })

        // #endif
    }
}

// rpx转px
export const rpxToPx = (rpx) => {
    const screenWidth = uni.getSystemInfoSync().screenWidth
    return (screenWidth * Number.parseInt(rpx)) / 750
}

// px转rpx
export const pxToRpx = (px) => {
    const screenWidth = uni.getSystemInfoSync().screenWidth
    return (750 * Number.parseInt(px)) / screenWidth
}

// 获取微信分享信息
export function getWxShare(options, imgUrl) {
    let pages = getCurrentPages(),
        page = pages[pages.length - 1],
        defaultOptions = {
            title: "HelloApp",
            path: `/${page.route}`,
            imageUrl: imgUrl,
            query: options.path.split('?')[1] + '&applet=1',
            promise: null,
            type: "message", // message 聊天界面，friend 朋友圈，favorite 收藏
            res: {},
            is_work: options.is_work,
            source: options.source
        };
    let params = {
        ...defaultOptions,
        ...options,
    };
    const {title, imageUrl, path, query, promise,is_work,source} = params;
    const message = {
        path,
        imageUrl,
        title,
        promise,
        is_work,
        source
    };
    const friend = {
        query: path + '&applet=1',
        imageUrl,
        title,
    };
    const favorite = {
        title,
        imageUrl,
        query: path + '&applet=1',
    };
    const shares = {
        message,
        friend,
        favorite,
    };
    const result = shares[params.type];
    console.log("source==", source)
    console.log(`获取微信小程序分享参数${params.type}:`, result);
    return params;
}


